package com.xbts.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.SysUserInvoiceInformation;
import com.xbts.system.service.ISysUserInvoiceInformationService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发票信息controller
 *
 * @author xbts
 */
@RestController
@RequestMapping("/system/userInvoiceInformation")
public class SysUserInvoiceInformationController extends BaseController
{
    @Autowired
    private ISysUserInvoiceInformationService userInvoiceInformationService;

    /**
     * 新增发票信息
     */
    @ApiOperation("新增发票信息")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insertOrUpdateInformation(@Validated @RequestBody SysUserInvoiceInformation sysUserInvoiceInformation)
    {
        return toAjax(userInvoiceInformationService.insertOrUpdateInformation(sysUserInvoiceInformation));
    }
    /**
     * 查询发票信息
     */
    @ApiOperation("查询发票信息")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo getUserInvoiceInformation()
    {
        return getDataTable(userInvoiceInformationService.getInvoiceInformation());
    }
    /**
     * 查询发票信息
     */
    @ApiOperation("查询发票信息")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/getByLoginUser")
    public AjaxResult getByLoginUser(String userId)
    {
        LambdaQueryWrapper<SysUserInvoiceInformation> queryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotEmpty(userId)){
            queryWrapper.eq(SysUserInvoiceInformation::getUserId, userId);
        } else {
            queryWrapper.eq(SysUserInvoiceInformation::getUserId, SecurityUtils.getUserId());
        }
        queryWrapper.orderByAsc(SysUserInvoiceInformation::getCreateTime);

        List<SysUserInvoiceInformation> list = userInvoiceInformationService.list(queryWrapper);
        if(list.size() <= 0){
            return success();
        }
        return success(list.get(0));
    }
    /**
     * 查询发票信息
     */
    @ApiOperation("查询发票信息")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable String id)
    {
        return success(userInvoiceInformationService.getById(id));
    }

}
